package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.DietRecord;
import com.example.diethealthsystem.service.DietRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/dietrecords")
public class DietRecordController {

    private final DietRecordService dietRecordService;

    @Autowired
    public DietRecordController(DietRecordService dietRecordService) {
        this.dietRecordService = dietRecordService;
    }

    @GetMapping
    public ResponseEntity<List<DietRecord>> getAllDietRecords() {
        List<DietRecord> dietRecords = dietRecordService.findAll();
        return new ResponseEntity<>(dietRecords, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<DietRecord> getDietRecordById(@PathVariable Long id) {
        return dietRecordService.findById(id)
                .map(dietRecord -> new ResponseEntity<>(dietRecord, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<DietRecord> createDietRecord(@RequestBody DietRecord dietRecord) {
        DietRecord createdDietRecord = dietRecordService.save(dietRecord);
        return new ResponseEntity<>(createdDietRecord, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<DietRecord> updateDietRecord(@PathVariable Long id, @RequestBody DietRecord dietRecord) {
        return dietRecordService.findById(id)
                .map(existingDietRecord -> {
                    existingDietRecord.setUser(dietRecord.getUser());
                    existingDietRecord.setFood(dietRecord.getFood());
                    existingDietRecord.setPortion(dietRecord.getPortion());
                    existingDietRecord.setEatTime(dietRecord.getEatTime());
                    DietRecord updatedDietRecord = dietRecordService.save(existingDietRecord);
                    return new ResponseEntity<>(updatedDietRecord, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteDietRecord(@PathVariable Long id) {
        if (dietRecordService.findById(id).isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        dietRecordService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<DietRecord>> getDietRecordsByUserId(@PathVariable Long userId) {
        List<DietRecord> dietRecords = dietRecordService.findByUserId(userId);
        if (dietRecords.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(dietRecords, HttpStatus.OK);
    }
} 